﻿/**  \file 

Open Source Initiative OSI - The MIT License (MIT):Licensing

The MIT License (MIT)
Copyright (c) 2012 

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), 
to deal in the Software without restriction, including without limitation the rights to use, 
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

//-------------------------------------------------------------------------

If you find any bugs or have suggestions, please let me know: ksacvet777@gmail.com




*/


#pragma once

#include <windows.h>

#include <stdexcept>
#include <string>
#include <iostream>

#include <assert.h>

#include "types.h"


#pragma warning(push)
#pragma warning(disable : 4290 )
#pragma warning(disable : 4996 )

namespace dlguif
{

	static const WCHAR* DLL_FILE_NAME = L"dlguif.dll";


	namespace detail 
	{

		class functions_s;
		class functions_form_s;
		class functions_ctrls_s;
		class functions_mainmenu_s;
		class functions_components_s;

	}




class Loader
{
private:

	HMODULE m_hdll;

	detail::functions_s* m_func;
	detail::functions_form_s* m_func_form;
	detail::functions_ctrls_s*   m_func_ctrls;
	detail::functions_mainmenu_s*     m_func_mainmenu;
	detail::functions_components_s*   m_func_components;


	void AGetProcFunc() throw (std::runtime_error);
	void AGetProcFormFunc() throw (std::runtime_error);
	void AGetProcFuncMainMenu() throw (std::runtime_error);
	void AGetProcFuncComponents() throw (std::runtime_error);
 

public:

	typedef void (__cdecl *FuncAssignText)(void* userptr, const char* data);
	
	//! if dllFileName is NULL then dll filename will be "dlguif.dll"
	Loader(const WCHAR* dllFileName = L"") throw (std::runtime_error);
	~Loader();
	
	static std::wstring SearchDll(const std::wstring& fname);

	static bool IsFileExists (const WCHAR* filename) 
	{
		return ::GetFileAttributesW( (LPCWSTR)filename) != DWORD(-1);
	}

	static inline void SetRectangle(RECT& r, int x, int y, int width, int height)
	{
		r.left = x;
		r.top  = y;
		r.right = r.left + width;
		r.bottom = r.top + height;
	}

	static inline RECT MakeRectangle(int x, int y, int width, int height)
	{
		RECT r;
		r.left = x;
		r.top  = y;
		r.right = r.left + width;
		r.bottom = r.top + height;
		return r;
	}

	static std::string event_type_to_string(const event_type et) throw (std::runtime_error);

	static inline DWORD MakeDelphiColor(BYTE red, BYTE green, BYTE blue) 
	{
		DWORD res = 0;
 
		res |= ((DWORD)blue)   <<  16;
		res |= ((DWORD)green)  <<   8;
		res |= ((DWORD)red)    <<   0;
 
		return res;
	}


	//------------------------------------------------------------

	//! sample filter:  text|*.txt|bitmap|*.bmp|any|*.*
	BOOL DialogSelectFile(const CHAR* caption, const CHAR* startDir, 
		const CHAR* filter, CHAR* dest, const int destsize);


	BOOL DialogSelectDirectory(const char* caption, const CHAR* startDir, CHAR* dest, const int destsize);

	BOOL DialogLogin(const char* caption, const char* loginEditText, const char* passwordEditText,
		const int maxLoginLen, const int maxPasswordLen, char* destLogin, char* destPassword, int destlen );


	BOOL DialogInputLine(const char* caption, const char* line, char* dest, const int destlen);

	
	
	BOOL DialogSelectColor(const char* caption, 
			int start_red, int start_green, int start_blue,
			int* dest_red, int* dest_green, int* dest_blue ) ;
	
	
	BOOL DialogSelectItem(const char* caption, const char* items, 
			int* destSelectItem);


	BOOL DialogInputText(const char* caption, const char* startText, 
			FuncAssignText funcdest, void* userptr);


	bool DialogInputVec3(const char* caption, float* x, float* y, float*z);
 
	struct vector3
	{
		float x, y, z;
	};

	bool DialogInputVec3(const char* caption, vector3* pv)
	{
		return DialogInputVec3( caption, &pv->x, &pv->y, &pv->z );
	}

	bool DialogSelectDirShell(const char* caption, char* dest, int destlen);
 
	//---------------------------------------------------------

	bool ProcessMessages();
	void SetGuiEventCallBack( FuncGuiEventCallBack func, void* user_context );
	void Terminate();
	void Run();
	void PostExit(int code);

	
	//---------------------------------------------------------
	
	void* CreateForm(
		const char* caption,
		const char* name,
		const RECT* recScreen
		);

	void* CreateForm (
		const char* caption,
		const char* name,
		int x, 
		int y, 
		int width,
		int height )
	{
		return CreateForm(caption, name, &MakeRectangle(x, y, width, height) );
	}


	void ShowForm(void* pForm);

	int ShowFormModal(void* pform);

	HRESULT CloseForm(void* pform);

	void* GetFormMainMenu(void* pForm);

	void* SearchForm(const char* formName);
	void DestroyForm(void** ppForm);

	bool IsFormHasMainMenu(void* pForm);

	int GetNumForms();

	inline HWND GetFormWindowHandle(void* pForm)
	{
		HWND res = GetWinControlHandle(pForm);
		return res;
	}

	void* GetApplication();

	


	//----------------------  create controls -----------------------------
 	
	HRESULT CreateGroupBox(
		void** ppOut,
		const char* caption,
		void* owner,
		void* parent,
		const RECT* recOnTheParent
		
		);
		

	HRESULT CreateButton(
		void** ppOut,
		const char* caption,
		void* owner,
		void* parent,
		const RECT* recOnTheParent);

	HRESULT CreateCheckBox(
		void** ppOut,
		void* owner,
		void* parent,
		const char* caption,
		int x, 
		int y,
		bool checked
		);


	HRESULT CreateLabel(
		void** ppOut,
		void* owner,
		void* parent,

		const char* caption,
		bool bold,

		int x,
		int y);
		
	 
	HRESULT	 CreateRadioItems (
		void** ppOut,
		void* owner,
		void* parent,	

		const char* caption,

		const char* items,
		int selectedIndex,

		int x,
		int y
	
		);
	
 
	HRESULT CreateEdit(
		void** ppOut,
		void* owner,
		void* parent,
 
		int x,
		int y,
		int width,

		const char* text);	
		
 
	HRESULT CreateSlider(
		void** ppOut,
		void* owner,
		void* parent,
 
		int x,
		int y,
		int len,
		bool vertical_Or_Horizontal,

		int min_pos,
		int max_pos,
		int posit 
 
		);
	
	HRESULT CreateMemo(
		void** ppOut,
		void* owner,
		void* parent,
		const RECT* recOnTheParent,
		const char* text		
		);
 
	HRESULT CreateListBox(
		void** ppOut,
		void* owner,
		void* parent,
		const RECT* recOnTheParent,
		const char* items, /// \n - separator
		int select_index
		
		);

	HRESULT CreateUpDown(
		void** ppOut,
		void* owner,
		void* parent,
		const RECT* recOnTheParent,

		bool orientVerticalOrHorizontal 
 
		);


	
	HRESULT CreateSpinEdit(
		void** ppOut,
		void* owner,
		void* parent,

		int x,
		int y,
		int width
	
	);

	HRESULT CreateComboBox(
		void** ppOut,
		void* owner,
		void* parent,

		const char* items,
		int selected_item,

		int x,
		int y,
		int width

		);

	HRESULT CreatePageControl (
		void** ppOut,
		void* owner,
		void* parent,

		const RECT* recOnTheParent
		
		);

	void* CreatePageControlTabSheet(
		void* pPageControl,
		const char* caption
		
		);

	void* CreatePopupMenu(void* owner, const char* name);
	void* CreatePopupMenuItem(void* pPopupMenu, const char* parent_item_caption, const char* caption);

	void* CreatePanel(void* owner, void* parent, const RECT* recOnTheParent);

	void* CreateTreeView(void* owner, void* parent, const RECT* recOnTheParent);
	void* CreateTreeViewItem(void* pTreeView, const char* parent_item_caption, 
		const char* caption, void* data);


	void* CreateProgressBar(void* owner, void* parent, const RECT* recOnTheParent);

	void* CreateSpliter(void* owner, void* parent, int width);

	void* CreateStatusBar(void* owner, void* parent);
	void* CreateStatusBarPanel(void* pStatusBar, const char* text, int width);



	//-------------------------------------------------------------------

	void PopupMenuPopup(void* pPopupMenu, int x, int y);


	//-------------------------------------------------------------------


	TWindowState GetFormWindowState(void* pForm);

	TFormBorderStyle GetFormBorderStyle(void* pForm);
	void SetFormBorderStyle(void* pForm, TFormBorderStyle bs);

	void SetFromWindowState(void* pForm, TWindowState ws);

	void SetFormStyle(void* pForm, TFormStyle fs);


	void SetFormOnResize(void* pForm, FuncOnResizeForm func, void* user_context);

	control_size_t GetFormClientSize(void* pForm);

	void SetFormIconFromFile(void* pForm, const char* filename);

	HDC GetFormDeviceContext(void* pForm);
	void* GetFormCanvas(void* pForm);



	//---------------------- main menu functions ------------------------

	void* CreateMainMenu(void* ownerForm);

	void* CreateMainMenuItem(
		void* pMainMenu,
		const char* parent_item_caption,
		const char* caption
		);



	//------------------------------------------------------------------


	int GetSliderPosition( void* pSlider );
	void SetSliderPosition( void* pSlider, int posit );

 
	void SetEditLine(void* pEdit, const char* line);
	void GetEditLine(void* pEdit, char* dest, int destlen);

	//void GetMemoText(void* pMemo, FUnc fnc, void* userptr);
	//void SetMemoText(void* pMemo, const char* text);







	//----------------  component functions --------------------

	void SetControlVisible( void* pControl, bool  visible);
	bool GetControlVisible( void* pControl );

	bool GetControlEnabled(void* pControl);
	void SetControlEnabled(void* pControl, bool value);

	void SetComponentTag(void* pComponent, int  tag);
	int GetComponentTag(void* pComponent );

	HWND GetWinControlHandle( void* pWinControl );
 

	void SetComponentCaption(void* pComponent, const char* caption);
	const char* GetComponentCaption(void* pComponent);

	void SetControlHint(void* pControl, const char* hint);

	void GetComponentClassNameCstr(void* pConponent, char* dest, int destlen);
	std::string GetComponentClassName(void* pComponent);

	RECT GetWinControlClientRect(void* pWinControl);

	anchors GetControlAnchors( void* pControl );
	void SetControlAnchors( void* pControl, const anchors* a );

	void SetControlPopupMenu(void* pControl, void* pPopupMenu);


	void SetProgressBarPosition(void* pProgressBar, int posit);
	int	 GetProgressBarPosition(void* pProgressBar);

	void   SetControlAlign(void* pControl, TAlign alg);
	TAlign GetControlAlign(void* pControl);

	void SetControlColor(void* pControl, DWORD color);
 

	void  SetTreeViewItemData(void* pTreeViewItem, void* data);
	void* GetTreeViewItemData(void* pTreeViewItem);

	void  SetTreeViewItemImageIndex(void* pTreeViewItem, int newIndex);
	int   GetTreeViewItemImageIndex(void* pTreeViewItem);

	void SetTreeVIewItemText(void* pTreeViewItem, const char* text);
	void GetTreeVIewItemText(void* pTreeViewItem, char* dest, const int destsize);

	
	//-----------------------------------------------


	void SetMainMenuItemChecked(void* pMainMenuItem, bool value);
	bool GetMainMenuItemChecked(void* pMainMenuItem);

	void SetMainMenuItemImageIndex(void* pMainMenuItem, int value);
	int GetMainMenuItemImageIndex(void* pMainMenuItem);

	void SetMainMenuItemCaption(void* pMainMenuItem, const char* caption);
	void GetMainMenuItemCaption(void* pMainMenuItem, char* dest, int destlen);

	void SetMainMenuItemEnabled(void* pMainMenuItem, bool value);
	bool GetMainMenuItemEnabled(void* pMainMenuItem);




 
	/*

	//void SetOnMessage(FuncOnMessage, void* user_context);




	struct component_info
	{
		int tag;
		RECT rectOnTheParent;
		BOOL visible;

		char name[32];
		char classname[32];
	};

	HRESULT GetComponentInfo(component_info* ciOut, void* pComponent);

	*/

};



}

#pragma warning(pop)
